package com.sj.purchase.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 宋开宗 on 2017/7/19 0019.
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    private DateUtils() {
    }

    /**
     * 字符串转换日期
     * @param dateStr
     * @return
     */
    public static Date dateFormat(String dateStr){
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取前一天日期
     * @return
     */
    public static Date yesterDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 获取本月最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        return addMilliseconds(ceiling(date, Calendar.MONTH), -1);// 进位月份然后减少1秒,即当月第一天最后一秒
    }

    /**
     * 获取当天23点59分59秒
     *
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        return addSeconds(addDays(date, 1), -1);
    }

    /**
     * 获取当天0点0分0秒
     * @param date
     * @return
     */
    public static Date getStart(Date date) {
        date = setHours(date, 0);
        date = setMinutes(date, 0);
        return setSeconds(date, 0);
    }

    /**
     * 获取当天23点59分59秒
     * @param date
     * @return
     */
    public static Date getEnd(Date date) {
        date = setHours(date, 23);
        date = setMinutes(date, 59);
        return setSeconds(date, 59);
    }

    /**
     * 获取当天0点0分0秒
     *
     * @param date
     * @return
     */
    public static Date getStartOfDay(Date date) {
        date = setHours(date, 0);
        date = setMinutes(date, 0);
        return setSeconds(date, 0);
    }


    public static Date getYesterday() {
        return DateUtils.getStartOfDay(DateUtils.addDays(new Date(), -1));
    }
}
